(function () {
    // 全局注册Liquid渲染
    var Liquid = window.liquidjs.Liquid;
    window.engine = new Liquid({
        extname: ".html",
        cache: true
    });
    window.engine.registerFilter("current_countrys", (item, key) => {
        if (!item) {
            return "https://imgcdn.oemsaas.cn/statics/country/flag/default.png";
        } else {
            const file = oemcart_countrys[item].country_code_2.toLowerCase();
            return `https://imgcdn.oemsaas.cn/statics/country/flag/${file}.png`;
        }
    });

    window.engine.registerFilter("money", (item, key) => {
        return formatMoney(parseFloat(item || 0), window.oemcart_currency);
    });

    function Moi(params) {
        //页面数据
        this.pageStorage = {};

        //事件
        this.event = {};

        //
        //添加购物车时间
        // addToCart:[]

        this.ProductPictureShake = true;
        this.ProductShake = true;

        this.globalpopupLayer;
    }

    Moi.prototype.addEvent = function (name, fun, isDefault) {
        const self = this;
        if (!this.event[name]) {
            this.event[name] = {
                isExecute: true,
                default: false,
                defaultFunction: null,
                params: {},
                preventDefault(event) {
                    //阻止默认
                    self.event[name].default = false;
                },
                stopPropagation() {
                    //阻止冒泡
                    self.event[name].isExecute = false;
                },
                fun: []
            };
        }

        if (isDefault && fun) {
            //    = fun.call(this, self.event);
            this.event[name].default = true;
            this.event[name].defaultFunction = fun; //  .bind(this, self.event)
            return;
        }

        this.event[name].fun.push(fun);
    };

    Moi.prototype.onEvent = function (name, params) {
        const event = this.event[name];
        if (!event) {
            return false;
        }
        event.params = params;
        let eventLength = 0;
        if (event.fun.length) {
            while (event.isExecute) {
                event.fun[eventLength].call(this, event);

                eventLength++;
                if (eventLength === event.fun.length) {
                    event.isExecute = false;
                }
            }
        }
        if (event.default) {
            event.defaultFunction.call(this, event);
        }
        //重置
        event.isExecute = true;

        return event;
    };

    // 表单方法
    Moi.prototype.form = function (id, rule) {
        //验证规则
        const copyRule = rule;

        // 获取fom接单
        const formNodes = $(id);

        //表单子元素
        const items = formNodes.find(`.mo-form-item`);

        //回调事件
        const callbackFu = {
            //获取input信息
            getInputValue: function (inputs, index) {
                const values = [];
                if (inputs.length) {
                    for (let i = 0; i < inputs.length; i++) {
                        const inputNode = inputs[i];

                        //绑定焦点事件
                        inputNode.addEventListener("focus", function (params) {
                            callbackFu.hideError(index);
                        });

                        values.push({ value: inputNode.value.trim(), index: index });
                    }
                }
                return values;
            },
            //获取所有value
            allValue: function () {
                const itemValues = {};
                for (let i = 0; i < items.length; i++) {
                    const itemNode = $(items[i]);
                    if (itemNode.attr("name")) {
                        // || `key_${i}`
                        const key = itemNode.attr("name");
                        var input = itemNode.find("input");
                        var textarea = itemNode.find("textarea");
                        itemValues[key] = callbackFu.getInputValue(input.length ? input : textarea, i);
                    }
                }
                return itemValues;
            },

            //隐藏错误信息
            hideError: function (index) {
                const itemNode = $(items[index]);
                if (itemNode.hasClass("form-show-error")) {
                    itemNode.removeClass("form-show-error");
                }
            },

            //显示错误
            showError: function (index, message) {
                const activeClas = "form-show-error";
                const itemNode = $(items[index]);
                const errorNode = itemNode.children(".mo-form-item-error");
                //初次错误追加元素
                if (!errorNode.length) {
                    return itemNode.addClass(activeClas).append(`<div class="mo-form-item-error">${message}</div>`);
                }

                //二次错误设置class
                if (!itemNode.hasClass(activeClas)) {
                    console.log("二次错误");
                    errorNode.html(message);
                    return itemNode.addClass(activeClas);
                }
            },

            //验证规则
            verifyRule(rules, value, index) {
                let returnStr = true;
                for (let i = 0; i < rules.length; i++) {
                    const rule = rules[i];

                    //验证必填

                    if (rule.required && !value.trim()) {
                        callbackFu.showError(index, rule.message);
                        returnStr = false;
                        return;
                    }

                    //是否有自定义验证
                    if (rule.validator) {
                        rule.validator(value, rules, function (msg) {
                            console.log(msg, index);
                            if (msg) {
                                callbackFu.showError(index, msg);
                                returnStr = false;
                            }
                        });
                    }
                }

                return returnStr;
            },

            //验证所有
            allVerify: function () {
                let status = true;

                const allValues = callbackFu.allValue();
                const keys = Object.keys(allValues);
                for (let i = 0; i < keys.length; i++) {
                    const key = keys[i];

                    const value = allValues[key].map((el) => el.value).toString();
                    const index = allValues[key].length && allValues[key][0].index;
                    //验证数据
                    if (copyRule[key] && !callbackFu.verifyRule(copyRule[key], value, index)) {
                        status = false;
                    }
                }

                return status;
            },

            //验证
            verify: function (name) {
                const allValues = callbackFu.allValue();

                const keys = Object.keys(allValues);

                const key = name;

                const value = allValues[key].map((el) => el.value).toString();
                // const value = allValues[key].toString();

                //验证数据

                if (copyRule[key] && !callbackFu.verifyRule(copyRule[key], value, keys.indexOf(name))) {
                    return false;
                }

                return true;
            }
        };

        return callbackFu;
    };

    //form value 数组变字符串
    Moi.prototype.paramsValueString = function (obj) {
        var keys = Object.keys(obj);
        keys.forEach((key) => {
            obj[key] = obj[key].map((el) => el.value).toString();
        });
        return obj;
    };

    //显示加载框
    Moi.prototype.nodeShowLoading = function (dom, str) {
        var selfDom = $(dom);
        var className = "mo-loading-warp";
        var old = selfDom.html();
        var style = "";
        if (str) {
            className = className + " " + str;
        }
        selfDom.addClass(className);
        if (str && str.indexOf("#") > -1) {
            style = `style="border-color:${str}; border-bottom-color: transparent;"`;
        }
        selfDom.html(`<div class="mo-loading" ${style}></div>`);
        return {
            //回调关闭加载样式
            close: function () {
                selfDom.removeClass(className);
                selfDom.html(old);
            }
        };
    };

    // //ajax
    // Moi.prototype.ajax = function (option) {
    //     $.ajax({
    //         ...option
    //     });
    // };

    //设置cookie
    //这是有设定过期时间的使用示例：
    //s20是代表20秒
    //h是指小时，如12小时则是：h12
    //d是天数，30天则：d30
    Moi.prototype.setCookie = function (name, value, time, path) {
        var strsec = getsec(time);
        var exp = new Date();
        exp.setTime(exp.getTime() + strsec * 1);
        document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString() + ";path=" + (path || "/");
    };

    function getsec(str) {
        var str1 = str.substring(1, str.length) * 1;
        var str2 = str.substring(0, 1);
        if (str2 == "s") {
            return str1 * 1000;
        } else if (str2 == "h") {
            return str1 * 60 * 60 * 1000;
        } else if (str2 == "d") {
            return str1 * 24 * 60 * 60 * 1000;
        }
    }

    //删除cookie
    Moi.prototype.delCookie = function (name) {
        var date = new Date();
        date.setTime(date.getTime() - 10000);
        document.cookie = name + "=a; expires=" + date.toGMTString();
    };

    //获取cookie
    Moi.prototype.getCookie = function (name) {
        var arr,
            reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if ((arr = document.cookie.match(reg))) return decodeURIComponent(arr[2]);
        else return null;
    };

    //获取链接参数
    Moi.prototype.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) {
            r[2] = r[2].replace("+", " ");
            return decodeURIComponent(r[2]);
        }
        return null; //返回参数值
    };

    //修改链接参数
    Moi.prototype.changeURLArg = function (url, arg, arg_val) {
        var pattern = arg + "=([^&]*)";
        var replaceText = arg + "=" + arg_val;
        if (url.match(pattern)) {
            var tmp = "/(" + arg + "=)([^&]*)/gi";
            tmp = url.replace(eval(tmp), replaceText);
            return tmp;
        } else {
            if (url.match("[?]")) {
                return url + "&" + replaceText;
            } else {
                return url + "?" + replaceText;
            }
        }
    };

    /**
     * @description: 设置链接参数
     * @param {*} url 拦截
     * @param {*} params get参数
     * @return {*} url
     */
    Moi.prototype.setUrlParams = function (url, params) {
        let newURL = url;
        const paramsKey = Object.keys(params);
        paramsKey.forEach((key) => {
            newURL = this.changeURLArg(newURL, key, params[key]);
        });
        return newURL;
    };

    //修改链接参数
    Moi.prototype.addURLParam = function (url, key, value) {
        var replaceText = key + "=" + value;
        if (url.match("[?]")) {
            return url + "&" + replaceText;
        } else {
            return url + "?" + replaceText;
        }
    };

    //提示框
    Moi.prototype.alert = function (options) {
        const template = `<div class="mo-confirm"><div class="mo-confirm-body"><div class="mo-title">${options.title}</div><div class="mo-handle"><div class="mo-handle-btn mo-handle-ok">${lang.general.yes}</div></div></div></div>`;
        const temp = $(template);
        // const closeNodes = temp.find(".mo-handle-close")[0];
        const okNodes = temp.find(".mo-handle-ok")[0];

        //关闭事件
        function close() {
            // closeNodes.removeEventListener("click", close, false);
            okNodes.removeEventListener("ok", ok, false);
            temp.remove();
        }

        //确定事件
        function ok() {
            options.ok && options.ok();
            close();
        }

        // closeNodes.addEventListener("click", close, false);
        okNodes.addEventListener("click", ok, false);

        $("body").append(temp);
    };

    //轻提示框
    Moi.prototype.alertInfo = function (options) {
        const defaultOption = {
            content: "",
            onClose: null,
            timer: 2000,
            ...options
        };
        const template = `<div class="mo-alret-model"><div class="mo-alret-body">${defaultOption.content}</div></div>`;
        const temp = $(template);
        $("body").append(temp);

        const modelBody = temp.find(".mo-alret-body");

        modelBody.addClass("animated bounceIn");

        setTimeout(() => {
            defaultOption.onClose && defaultOption.onClose.call(this, {});

            //取消动画
            modelBody.addClass("animated bounceOut");
            setTimeout(function () {
                temp.remove();
            }, 500);
        }, defaultOption.timer);
    };

    // 消息框
    Moi.prototype.message = function (options) {
        const defaultOption = {
            type: "success", // success，error，info
            content: "content",
            onClose: null,
            link: "",
            timer: 2000,
            ...options
        };
        let icon = "";

        switch (defaultOption.type) {
            case "success":
                icon = `<svg t="1641532926783" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9539" width="200" height="200"><path d="M512 25.6a486.5408 486.5408 0 0 0-189.3248 934.592 486.528 486.528 0 0 0 378.6496-896.384A483.328 483.328 0 0 0 512 25.6m0-25.6A512 512 0 1 1 0 512 512 512 0 0 1 512 0z" fill="#29BB9D" p-id="9540"></path><path d="M274.092113 486.674451m9.050966 9.050967l162.917403 162.917402q9.050967 9.050967 0 18.101934l0 0q-9.050967 9.050967-18.101934 0l-162.917402-162.917403q-9.050967-9.050967 0-18.101933l0 0q9.050967-9.050967 18.101933 0Z" fill="#29BB9D" p-id="9541"></path><path d="M775.375724 350.881006m-9.050967 9.050967l-316.783838 316.783838q-9.050967 9.050967-18.101933 0l0.009051 0.009051q-9.050967-9.050967 0-18.101934l316.783838-316.783838q9.050967-9.050967 18.101933 0l-0.009051-0.009051q9.050967 9.050967 0 18.101934Z" fill="#29BB9D" p-id="9542"></path></svg>`;
                break;
            case "error":
                icon = `<svg t="1635319085856" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="29395" width="200" height="200">
                <path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#EE4550" p-id="29396"></path>
                <path d="M548.266667 512.341333l126.976 127.232a25.642667 25.642667 0 0 1-36.224 36.309334L512 548.650667l-126.976 127.232a25.642667 25.642667 0 0 1-36.224-36.309334l126.976-127.232-126.976-127.232a25.6 25.6 0 0 1 0.128-36.010666 25.6 25.6 0 0 1 36.138667 0l126.933333 126.933333 126.976-127.232a25.642667 25.642667 0 0 1 36.224 36.309333l-126.933333 127.232z" fill="#FFFFFF" p-id="29397"></path>
              </svg>`;
                break;
            case "info":
                icon = `<svg t="1635319120477" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="29528" width="200" height="200">
                <path
                  d="M512 0a512 512 0 1 0 512 512 512 512 0 0 0-512-512z m31.552 757.824a52.096 52.096 0 0 1-35.648 13.696 53.952 53.952 0 0 1-36.544-13.568 47.488 47.488 0 0 1-15.616-37.888 48.832 48.832 0 0 1 15.104-36.352 51.2 51.2 0 0 1 37.056-14.72 50.368 50.368 0 0 1 51.2 51.2 47.872 47.872 0 0 1-15.424 37.696z m128-317.504a156.096 156.096 0 0 1-28.096 37.888q-16.256 15.936-58.432 53.632a238.208 238.208 0 0 0-18.688 18.688 75.904 75.904 0 0 0-10.432 14.72 72.448 72.448 0 0 0-5.312 13.376q-1.856 6.4-5.632 23.488a38.4 38.4 0 0 1-40.768 35.648 41.664 41.664 0 0 1-30.016-11.648 45.76 45.76 0 0 1-12.16-34.624 121.6 121.6 0 0 1 32.576-86.912 543.488 543.488 0 0 1 39.744-37.888q21.952-19.2 32-28.992a99.136 99.136 0 0 0 16.448-21.76 52.544 52.544 0 0 0 6.4-26.048 60.608 60.608 0 0 0-20.416-46.272 74.56 74.56 0 0 0-52.608-18.816 72.768 72.768 0 0 0-55.552 19.2 151.488 151.488 0 0 0-30.208 56.064q-11.648 38.4-44.224 38.4a43.456 43.456 0 0 1-32.384-13.568 41.344 41.344 0 0 1-13.184-29.312 123.776 123.776 0 0 1 20.928-65.984 160.896 160.896 0 0 1 60.992-55.36 192 192 0 0 1 93.632-21.952 199.872 199.872 0 0 1 87.744 18.368 142.272 142.272 0 0 1 58.816 49.856 122.304 122.304 0 0 1 20.736 68.544 105.984 105.984 0 0 1-11.84 51.2z"
                  fill="#333333" p-id="29529"></path>
              </svg>`;
                break;
        }

        const template = `<div class=mo-modal><div class=mo-modal-mask></div><div class=mo-modal-body><div class=mo-modal-warp>${icon} <span class="message-text">${defaultOption.content}</span></div></div></div>`;
        const temp = $(template);
        $("body").append(temp);

        const modelBody = temp.find(".mo-modal-body");
        modelBody.addClass("animated bounceIn");

        setTimeout(() => {
            modelBody.addClass("animated bounceOut"); //取消动画
            setTimeout(function () {
                //关闭弹窗
                temp.remove();
                //是否存在链接跳转
                if (defaultOption.link) {
                    window.location.href = defaultOption.link;
                    return;
                }
                defaultOption.onClose && defaultOption.onClose.call(this, {});
            }, 500);
        }, defaultOption.timer);
    };

    //询问框
    Moi.prototype.confirm = function (options) {
        const defaultOption = {
            title: "content",
            okText: lang.general.yes,
            cancelText: lang.general.no,
            ...options
        };

        const template = `<div class="mo-confirm"><div class="mo-confirm-body"><div class="mo-title">${defaultOption.title}</div><div class="mo-handle"><div class="mo-handle-btn mo-handle-ok">${defaultOption.okText}</div><div class="mo-handle-btn mo-handle-close">${defaultOption.cancelText}</div></div></div></div>`;
        const temp = $(template);
        const closeNodes = temp.find(".mo-handle-close")[0];
        const okNodes = temp.find(".mo-handle-ok")[0];

        //关闭事件
        function close() {
            closeNodes.removeEventListener("click", close, false);
            okNodes.removeEventListener("ok", ok, false);
            temp.find(".mo-confirm-body").addClass("animated bounceOut");
            setTimeout(function () {
                temp.remove();
            }, 500);
        }

        //关闭事件
        function ok() {
            options.ok && options.ok();
            close();
        }

        closeNodes.addEventListener("click", close, false);
        okNodes.addEventListener("click", ok, false);

        $("body").append(temp);

        temp.find(".mo-confirm-body").addClass("animated bounceIn");
    };

    //定位到元素

    Moi.prototype.windowPositionNode = function (id) {
        $("html,body").animate({ scrollTop: $(id).offset().top }, 200);
    };

    //文字提示框
    Moi.prototype.tooltip = function (options) {
        const defaultOption = {
            placement: "left",
            el: "",
            timer: 1000,
            space: 0,
            content: "",
            ...options
        };

        const cssOption = {
            opacity: 1,
            transform: "scale(1)",
            visibility: "visible"
        };

        const template = `<div class="mo-tooltip mo-tooltip-placement-${defaultOption.placement}">
    <div class="mo-tooltip-content">
      <div class="mo-tooltip-arrow"><span class="mo-tooltip-arrow-content"></span></div>
      <div class="mo-tooltip-inner" role="tooltip">${defaultOption.content}</div>
    </div>
  </div>`;
        const temp = $(template);

        const node = $(defaultOption.el);

        const top = node.offset().top;
        const width = node.width();
        const height = node.height();
        const left = node.offset().left;

        //追加页面元素
        $("body").append(temp);

        const tipWidth = temp.innerWidth();
        const tipHeight = temp.innerHeight();

        switch (defaultOption.placement) {
            case "left":
                temp.css({
                    ...cssOption,
                    left: left - tipWidth - defaultOption.space,
                    top: top - Math.floor(height / 2)
                });
                break;
            case "right":
                temp.css({
                    ...cssOption,
                    left: left + width + defaultOption.space,
                    top: top - Math.floor(height / 2)
                });
                break;
            case "top":
                temp.css({
                    ...cssOption,
                    left: left,
                    top: top - tipHeight - defaultOption.space
                });
                break;
            case "bottom":
                temp.css({
                    ...cssOption,
                    left: left,
                    top: top + height + defaultOption.space
                });
                break;
        }

        setTimeout(function () {
            temp.css({
                opacity: 0,
                transform: "scale(0)"
            });
            setTimeout(function () {
                temp.remove();
            }, 500);
        }, 1000);
    };

    //post 请求
    Moi.prototype.ajax = function (option) {
        const slef = this;
        option.url = `/homeapi${option.url}`;
        // console.log(option)
        $.ajax({
            headers: {
                // Token: slef.getCookie("Token"),
                "Content-Type": "application/json;charset=UTF-8"
            },
            // data: JSON.stringify(data),
            dataType: "json",
            ...option
        });
    };

    //post 请求
    Moi.prototype.post = function (url, data, option) {
        url = `/homeapi/${url}`;
        this.ajax({
            url: url,
            type: "post",
            data: JSON.stringify(data),
            ...option
        });
    };

    //get 请求
    Moi.prototype.get = function (url, data, option) {
        this.ajax({
            url: url,
            type: "get",
            data: data,
            ...option
        });
    };

    //del 请求
    Moi.prototype.del = function (url, data, option) {
        this.ajax({
            url: url,
            type: "delete",
            data: JSON.stringify(data),
            ...option
        });
    };

    //设置页面数据
    Moi.prototype.setPageStorage = function (key, option) {
        this.pageStorage[key] = JSON.parse(JSON.stringify(option));
    };

    //获取页面数据
    Moi.prototype.getPageStorage = function (key, defaultParams) {
        return this.pageStorage[key] || defaultParams;
    };

    //获取页面数据
    Moi.prototype.quantity = function (option) {
        const quantityDom = option.dom;
        const valueDom = quantityDom.find(".product-calculator-value");
        const handleDom = quantityDom.find(".product-calculator-handle");
        handleDom.click(function () {
            const type = $(this).data("type");
            let value = Number(valueDom.html());

            if (type === "reduce" && value !== 1) {
                valueDom.html(--value);
            }
            if (type === "add") {
                valueDom.html(++value);
            }

            option.change && option.change.call(this, { value: value, type: type });
        });
    };

    //监听A标签调整
    Moi.prototype.bindRedirectHref = function (node) {
        const self = this;
        const theme_id = this.getUrlParam("theme_id");
        // document.get
        if (theme_id) {
            const ADOM = node || $("a");
            ADOM.on("click", function (e) {
                e.preventDefault();
                const href = $(this).attr("href");

                if (href === "javascript:;") {
                    // self.alertInfo({ content: $(this).data("tips") || "请添加" });
                    return;
                }

                if (href.indexOf("?") === -1) {
                    location.href = `${href}?theme_id=${theme_id}`;
                } else {
                    location.href = `${href}&theme_id=${theme_id}`;
                }
            });
        }
    };

    //跳转页面
    Moi.prototype.href = function (URL) {
        let url = URL;
        const theme_id = this.getUrlParam("theme_id");
        //是否有主题id
        if (theme_id) {
            if (url) {
                const splitStr = url.indexOf("?") === -1 ? "?" : "&";
                url = `${URL}${splitStr}theme_id=${theme_id}`;
            } else {
                this.alertInfo({ content: "请添加链接" });
                return false;
            }
        }
        location.href = url;
    };

    Moi.prototype.appendLoading = function ({ size = 50, container, theme, position } = {}) {
        $(container).css('pointer-events', 'none')
        const style = document.createElement("style");
        style.innerHTML = `
           #common_tool-loading {
              position: ${position ? position : container ? "absolute" : "fixed"};
              top: 0; left: 0; bottom: 0; right: 0;
              background: ${theme ? (theme === "dark" ? "rgba(0, 0, 0, .8)" : "#ffffffe6") : ""};
              display: flex;
              align-items: center;
              justify-content: center;
              transition: opacity .2s linear;
           }
           #common_tool-loading svg {
              animation: common_tool-loading-spin 1s linear infinite;
              fill: ${theme && theme === "dark" ? "#fff" : "#666"}
           }
           @keyframes common_tool-loading-spin {
              from { transform: rotate(0deg); }
              to { transform: rotate(360deg); }
           }
        `;
        const loading = `
           <div id="common_tool-loading">
              <svg width="${size}" height="${size}" t="1644634233141" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="11473"><path d="M270.4 214.4C336 160 420 128 512 128c212 0 384 172 384 384h64c0-247.2-200.8-448-448-448-107.2 0-205.6 37.6-282.4 100l40.8 50.4z" p-id="11474"></path></svg>
           </div>
        `;
        document.head.appendChild(style);
        $(container || "body").append(loading);
    };

    Moi.prototype.removeLoading = function (container) {
        $(container).css('pointer-events', '')
        const $loading = $(`${container || "body"} #common_tool-loading`).css("opacity", 0);
        setTimeout(() => $loading.remove(), 200);
    };
    Moi.prototype.popup = function (option) {
        const defaultOption = { load: true, animated: true, ...option };
        const temp = $(`<div class="mo-popup"><div class="mo-popup-mask popup-claer"></div><div class="mo-popup-body"><div class="mo-popup-content"></div></div></div>`);
        const content = temp.find(".mo-popup-content");
        let load = false;

        if (defaultOption.load) {
            load = moi.nodeShowLoading(content, "mo-loading-black");
        }

        const returnOption = {
            temp: temp,
            load: load,
            showLoading() {
                returnOption.load = moi.nodeShowLoading(temp.find(".mo-popup-content"), "mo-loading-black");
                return load;
            },
            hideLoading() {
                returnOption.load && returnOption.load.close();
            },
            close(callback) {
                defaultOption.animated && content.addClass("animated bounceOutUp");
                setTimeout(function () {
                    temp.remove();
                    callback && callback();
                }, 800);
            },
            addContent(html) {
                defaultOption.load && load.close();
                content.append(html);
                defaultOption.animated && content.addClass("animated bounceInDown");
                const closeNodes = temp.find(".popup-claer");

                closeNodes.on("click", function () {
                    returnOption.close();
                });
            }
        };

        returnOption.showLoading();
        $("body").append(temp);

        return returnOption;
    };

    Moi.prototype.createScript = function (URL, callback) {
        //当前url的script
        const currentScript = $("script[src='" + URL + "']");

        //是否存在；存在直接执行
        if (currentScript.length) {
            callback && callback();

            return;
        }

        //创建一个script标签
        const script = document.createElement("script");
        script.setAttribute("type", "text/javascript");
        script.setAttribute("src", URL);
        script.onload = function () {
            callback && callback();
        };
        document.body.appendChild(script);
    };

    /**
     *
     * @param {*} videoId 视频id
     * @param {*} vedioContainer 视频容器
     * @param {*} height 视频高度，默认300
     * @param {*} width 视频宽度，默认全屏
     * @param {*} config 配置文件，播放参数
     */
    Moi.prototype.youtubeVideoPlay = function (videoId, vedioContainer, height = 300, width = window.innerWidth, config = {}) {
        if (!videoId) throw "videoId不能为空";
        if (!vedioContainer) throw "视频容器不能为空";
        this.createScript("https://www.youtube.com/iframe_api", () => {
            window.onYouTubeIframeAPIReady = function () {
                new YT.Player(vedioContainer, {
                    height,
                    width,
                    videoId,
                    playerVars: {
                        ...config,
                        autohide: 0,
                        branding: 0,
                        cc_load_policy: 0,
                        controls: 1,
                        fs: 0,
                        iv_load_policy: 3,
                        modestbranding: 1,
                        playsinline: 1,
                        quality: "hd720",
                        rel: 0,
                        showinfo: 0,
                        loop: 1,
                        wmode: "opaque",
                        mute: 1
                    },
                    events: {
                        onReady: function (event) {
                            event.target.playVideo();
                        }
                    }
                });
            };
            if (YT.Player) {
                window.onYouTubeIframeAPIReady();
            }
        });
    };

    //关闭快速预览
    window.quickViewClose = function (callback) {
        if (moi.globalpopupLayer) {
            moi.globalpopupLayer.close(callback);
        }
    };
    //快速预览加入购物车
    window.quickViewCart = function (params) {
        quickViewClose(function () {
            miniCart();
            const unm = parseInt($("#cart_number").text() || 0) + params.quantity;
            $("#cart_number")
                .show()
                .html(unm > 99 ? 99 + "+" : unm);
            $(".minicart").addClass("minicart_on");
        });
    };
    //快速预览立即购买
    window.quickViewBuy = function (url) {
        quickViewClose(function () {
            moi.href(url);
        });
    };
    //查看详情
    window.quickViewDetail = function (url) {
        quickViewClose(function () {
            moi.href(url);
        });
    };

    //显示快速预览
    window.quickViewLoad = function (height) {
        const classStr = themeConfig.global.product_quickview_type == "normal" ? "normal-quick-iframe" : "simple-quick-iframe";
        const quickIframe = moi.globalpopupLayer.temp.find("#quick-iframe");
        const iframeWrapNode = moi.globalpopupLayer.temp.find(".quick-iframe-wrap");
        moi.globalpopupLayer.temp.find(".quick-iframe-wrap-loading").remove();
        quickIframe.height(height);
        quickIframe.addClass(classStr);

        setTimeout(function () {
            if (height > iframeWrapNode.innerHeight()) {
                console.log(iframeWrapNode, iframeWrapNode.innerHeight());
                quickIframe.attr("scrolling", "auto");
            }
        }, 500);
    };

    //弹出商品预览
    Moi.prototype.popupProductPicture = function (option) {
        if (!this.ProductPictureShake) {
            return false;
        }
        const theme_id = this.getUrlParam("theme_id");
        // from
        this.globalpopupLayer = this.popup();
        this.globalpopupLayer.addContent(`<div class="quick-iframe-wrap"><div class="quick-iframe-wrap-loading"></div><iframe class="quick-iframe" scrolling="no" id="quick-iframe" src="/products/${option.handle}?&quick_view=1&data_from=${option.data_from}"></iframe></div>`);
    };

    //弹出商品选择加入购物车
    Moi.prototype.popupProduct = function (option) {
        if (!this.ProductShake) {
            return false;
        }

        const self = this;

        const lang = window.lang;

        const popupLayer = this.popup();
        this.ProductShake = false;
        getProductInfo(option.handle, function (ret) {
            const retData = ret.data.product;
            self.ProductShake = true;
            //添加购物车
            function addCart(load) {
                moi.ajax({
                    url: "/cart/add",
                    type: "post",
                    data: JSON.stringify({
                        product_id: retData.id,
                        sku_code: retData.variant.sku_code,
                        quantity: 1,
                        data_from: self.getUrlParam("data_from")
                    }),
                    complete() {
                        load && load.close();
                    },
                    success: function (data) {
                        if (!data.code) {
                            temp.remove();
                            miniCart();
                            const unm = parseInt($("#cart_number").text() || 0) + 1;
                            $("#cart_number")
                                .show()
                                .html(unm > 99 ? 99 + "+" : unm);
                            $(".minicart").addClass("minicart_on");
                            oemasaasAddCardScriptScope && oemasaasAddCardScriptScope();
                        } else {
                            return self.alert({
                                title: data.msg
                            });
                        }
                    }
                });
            }

            if (retData.variants.length === 1) {
                popupLayer.close();
                addCart();
                return;
            }

            engine
                .parseAndRender($("#omesaas-popup-addPurchase").html(), {
                    product: retData,
                    lang: lang
                })
                .then(function (html) {
                    popupLayer.addContent(html);
                    const temp = popupLayer.temp;
                    const productDetail = new omesaasProduct({
                        product: retData,
                        id: temp.find("#buy-sku-option")
                    });

                    self.quantity({
                        dom: temp.find(".mo-quantity"),
                        change(val) {
                            productDetail.setSelectedVal({ quantity: val.value });
                        }
                    });

                    //加入购物车
                    temp.find("[data-type='cart']").click(function () {
                        if (!productDetail.verifyOption("cart")) {
                            return false;
                        }

                        var load = self.nodeShowLoading(this, "mo-loading-black");
                        productDetail.addCart(
                            function () {
                                load.close();
                            },
                            function (data) {
                                if (!data.code) {
                                    popupLayer.close(function () {
                                        $(".minicart").addClass("minicart_on");
                                    });
                                } else {
                                    return self.alert({
                                        title: data.msg
                                    });
                                }
                            }
                        );
                    });
                    //直接购买
                    temp.find("[data-type='buy_now']").click(function () {
                        if (!productDetail.verifyOption("buy")) {
                            return false;
                        }

                        var load = self.nodeShowLoading(this);

                        productDetail.buynow(
                            function () {
                                load.close();
                            },
                            function (ret) {
                                if (!ret.code) {
                                    self.href(ret.data.checkout_url);
                                } else {
                                    return self.alert({
                                        title: ret.msg
                                    });
                                }
                            }
                        );
                    });
                });
        });
    };

    //过滤设置当前语言
    Moi.prototype.getLanguage = function (language) {
        if (language[oemcart_website_language]) {
            return language[oemcart_website_language];
        }
        return "";
    };

    //格式化时间
    Moi.prototype.formatDate = function (date, format) {
        let we = date.getDay(); // 星期
        let z = getWeek(date); // 周
        let qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度
        const opt = {
            "Y+": date.getFullYear().toString(), // 年
            "m+": (date.getMonth() + 1).toString(), // 月(月份从0开始，要+1)
            "d+": date.getDate().toString(), // 日
            "H+": date.getHours().toString(), // 时
            "M+": date.getMinutes().toString(), // 分
            "S+": date.getSeconds().toString(), // 秒
            "q+": qut // 季度
        };
        // 中文数字 (星期)
        const week = {
            0: "日",
            1: "一",
            2: "二",
            3: "三",
            4: "四",
            5: "五",
            6: "六"
        };
        // 中文数字（季度）
        const quarter = {
            1: "一",
            2: "二",
            3: "三",
            4: "四"
        };
        if (/(W+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? "星期" + week[we] : "周" + week[we]) : week[we]);
        if (/(Q+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? "第" + quarter[qut] + "季度" : quarter[qut]);
        if (/(Z+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? "第" + z + "周" : z + "");
        for (let k in opt) {
            let r = new RegExp("(" + k + ")").exec(format);
            // 若输入的长度不为1，则前面补零
            if (r) format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, "0"));
        }
        return format;
    };

    //生成随机数
    Moi.prototype.randomInteger = function (minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            default:
                throw new Error("仅允许传入1~2个参数");
        }
    };
    //时区时间时间戳格式
    Moi.prototype.formatZoneDate = function (value, format) {
        if (!value) {
            return "";
        }
        // return value.split('T')[0]
        // let time = 0;
        // if (typeof value === "string") {
        //     // return this.formatZoneDateStr(value,format)
        //     time = new Date(value).getTime();
        // } else {
        //     time = value * 1000; // new Date().getTime();
        // }
        // const sys_time = oemcart_time_zone;
        // const reg = RegExp(/-/); //这儿注意有坑。。如果包含了-则不需要处理如果包含了+需要加上-, 后端php无法处理这个问题所以这儿反向判断了。。
        // const data = reg.test(sys_time) ? Number(sys_time.split(/[-|+]/)[1]) : -Number(sys_time.split(/[-|+]/)[1]);
        // const newData = ((isNaN(data) ? 0 : data) - moment().parseZone().utcOffset() / 60) * 60 * 60 * 1000 + time;
        // console.log(value,new Date(newData))
        // return this.formatDate(new Date(newData), format);

        const sys_time = oemcart_time_zone;
        const reg = RegExp(/-/); //这儿注意有坑。。如果包含了-则不需要处理如果包含了+需要加上-, 后端php无法处理这个问题所以这儿反向判断了。。
        const data = reg.test(sys_time) ? Number(sys_time.split(/[-|+]/)[1]) : -Number(sys_time.split(/[-|+]/)[1]);
        const timeZone = parseInt(new Date().getTimezoneOffset() / 60) + parseInt(data);
        return this.formatDate(new Date(timeZone * 60 * 60 * 1000 + value * 1000), format);
    };

    //节流
    Moi.prototype.throttle = function (func, wait) {
        //之前的时间戳
        let old = 0;
        return function () {
            //获取当前的时间戳
            let now = new Date().valueOf();
            if (now - old > wait) {
                //立即执行
                func.apply(this, arguments);

                old = now;
            }
        };
    };

    /**
     * @description: 预览图片
     * @param {*} urls 图片集合
     * @param {*} index 当前预览
     * @param {*} self 点击元素
     * @return {*}
     */
    Moi.prototype.pictureImages = function (urls, index, self) {
        const tempHtml = $(`
        <div class="view-picture">
        <div class="view-picture-swiper" id="picture-swiper-container">
          <ul class="swiper-wrapper" style="width:100%"></ul>
        </div>
        <div class="view-picture-operate">
          <div class="operate-arrow-right" id='picture-swiper-prev'>
            <svg t="1635160440262" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1353" width="20" height="20"><path d="M511.609097 961.619254" p-id="1354"></path><path d="M360.749204 511.010463 360.749204 511.010463l382.080022-382.074905c14.762215-14.788821 14.762215-38.777165 0-53.585429-14.812357-14.788821-38.799678-14.788821-53.607942 0L283.415908 481.201553c-1.107217 0.867764-2.190899 1.824555-3.206018 2.843768-14.788821 14.808264-14.788821 38.795585 0 53.585429l408.226518 408.230612c14.808264 14.807241 38.795585 14.807241 53.584406 0 14.785751-14.767332 14.785751-38.754652 0-53.562916L360.749204 511.010463 360.749204 511.010463zM360.749204 511.010463" p-id="1355"></path></svg>
          </div>
          <div class="operate-arrow-close" id="operate-close">
            <svg t="1633405599567" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1984" width="20" height="20"><path d="M453.44 512L161.472 220.032a41.408 41.408 0 0 1 58.56-58.56L512 453.44 803.968 161.472a41.408 41.408 0 0 1 58.56 58.56L570.56 512l291.968 291.968a41.408 41.408 0 0 1-58.56 58.56L512 570.56 220.032 862.528a41.408 41.408 0 0 1-58.56-58.56L453.44 512z" p-id="1985"></path></svg>
          </div>
          <div class="operate-arrow-left" id='picture-swiper-next'>
            <svg t="1633330597342" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2631" width="20" height="20"><path d="M765.7 486.8L314.9 134.7c-5.3-4.1-12.9-0.4-12.9 6.3v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1c16.4-12.8 16.4-37.6 0-50.4z" p-id="2632"></path></svg>
          </div>
        </div>
      </div>
        `);
        let swiperSlideHtml = "";

        for (let i = 0; i < urls.length; i++) {
            swiperSlideHtml += `<li class="swiper-slide"><img src="${urls[i]}"></li>`;
        }

        tempHtml.find(".swiper-wrapper").append(`<li class="swiper-slide"><img src="${urls[index]}"></li>`);

        const top = self.offset().top - $(document).scrollTop();
        const left = self.offset().left;
        const height = self.height();
        const width = self.width();

        tempHtml.css({ height: height, top: top, left: left, width: width, opacity: 0.1 });
        $("body").append(tempHtml);

        setTimeout(function () {
            tempHtml.css({ height: "100%", top: 0, left: 0, width: "100%", opacity: 1 });
        }, 100);

        setTimeout(function () {
            tempHtml.css({ backgroundColor: "rgba(0, 0, 0, 0.6)" });

            tempHtml.addClass("view-picture-operate-show");

            tempHtml.find(".swiper-wrapper").html(swiperSlideHtml);

            new Swiper("#picture-swiper-container", {
                loop: false,
                initialSlide: index,
                autoHeight: true,
                navigation: {
                    nextEl: "#picture-swiper-next",
                    prevEl: "#picture-swiper-prev"
                }
            });
        }, 800);

        tempHtml.find("#operate-close").click(function () {
            tempHtml.addClass("view-picture-operate-hide");
            tempHtml.css({ opacity: 0.1 });
            setTimeout(function () {
                tempHtml.remove();
            }, 400);
        });
    };

    window.moi = new Moi();

    function getWeek(dateTime) {
        let temptTime = new Date(dateTime.getTime());
        // 周几
        let weekday = temptTime.getDay() || 7;
        // 周1+5天=周六
        temptTime.setDate(temptTime.getDate() - weekday + 1 + 5);
        let firstDay = new Date(temptTime.getFullYear(), 0, 1);
        let dayOfWeek = firstDay.getDay();
        let spendDay = 1;
        if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1;
        firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay);
        let d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000);
        let result = Math.ceil(d / 7);
        return result;
    }

    function getProductInfo(handle, success) {
        moi.ajax({
            url: `/products/${handle}`,
            type: "get",
            success: function (ret) {
                if (!ret.code) {
                    success && success.call(this, ret);
                } else {
                    return moi.alert({
                        title: ret.msg
                    });
                }
            }
        });
    }

    window.oemsaasQuickview = moi.throttle(function (e) {
        e.stopPropagation();
        e.preventDefault();
        moi.popupProductPicture({
            handle: $(this).data("handle")
        });
    }, 1000);
})();

(function () {
    function waterfalls(option) {
        const self = this;

        this.dom = $(option.id);

        //resize 配置
        this.resizeConfig = option.resizeConfig;

        //多少列
        this.currentColumnConfig = this.resizeWidth($(window).width()); //option.columns || 3;

        //每列间距
        this.columnsGutter = "20px";

        //渲染方式
        this.renderType = option.renderType || "Js";

        //每行间距
        this.rowGutter = "20px";

        //节点 $(option.id)
        this.fallNode = null;
        //子元素类名
        this.childrenClassName = option.childrenClass || ".mo-waterfall-column";

        //距离底部高度
        this.bottomDistance = option.bottomDistance || 30;

        //渲染方法
        this.render = option.render;

        //获取图片数据key
        this.imgKey = option.imgKey || false;

        //窗口可见高度
        this.windowH = $(window).height();

        //滚动到底部
        this.scrollBottom = option.scrollBottom;

        //渲染完成
        this.renderComplete = option.renderComplete;

        //加载样式
        this.loadHtml = $('<div class="mo-waterfall-status-box"><div class="loading-box"></div></div>');

        //加载所有样式

        // this.allHtml = $('<div class="mo-waterfall-allLoad">价值</div>')

        //数据缓存
        this.storage = [];

        //添加高度
        this.addHeight = option.addHeight;

        //初始化·
        this.initDom();

        //滚动事件监听
        $(window).scroll(function () {
            // const theme_id = moi.getUrlParam("theme_id");
            // if (!theme_id) {
            self.scroll.call(this, self);
            // }
        });

        //页面大小变化监听
        $(window).resize(function () {
            const width = $(window).width();
            const columns = self.resizeWidth(width);
            if (columns && columns.columns !== self.currentColumnConfig.columns) {
                self.refresh(columns);
            }
        });
    }

    //获取宽度布局
    waterfalls.prototype.resizeWidth = function (width) {
        for (let i = 0; i < this.resizeConfig.length; i++) {
            const config = this.resizeConfig[i];
            if (config.min <= width && (width <= config.max || !config.max)) {
                return config;
            }
        }
        return false;
    };

    //重新初始化
    //渲染数据
    waterfalls.prototype.refresh = function (columns) {
        this.currentColumnConfig = columns;
        this.dom.empty();
        this.initDom();
        this.storageRender(this.storage);
    };

    //渲染默认数据
    waterfalls.prototype.RenderDefaultData = function (data, moreText) {
        const list = [];
        for (let index = 0; index < this.currentColumnConfig.columns * 3; index++) {
            list.push(data);
        }
        this.storageRender(list);
        this.showMore(moreText, "javascript:;");
    };

    //初始化dom渲染列
    waterfalls.prototype.initDom = function () {
        this.dom.append(`<div class="mo-waterfall-warp"></div>`);

        this.fallNode = this.dom.children(".mo-waterfall-warp");
        //追加列数据
        for (let i = 0; i < this.currentColumnConfig.columns; i++) {
            const element = $(`<div class="mo-waterfall-column" style="--row-gutter:${this.currentColumnConfig.rowGutter}"></div>`);
            this.fallNode.append(element);
        }

        //设置css 变量属性
        this.fallNode.attr("style", `--columns:${this.currentColumnConfig.columns};--column-gutter:${this.currentColumnConfig.columnsGutter}`);

        //子元素
        this.childs = this.getChilds();
    };

    //追加数据
    waterfalls.prototype.addStorage = function (data) {
        this.storage = [...this.storage, ...data];
        this.storageRender(data);
    };

    waterfalls.prototype.getImageHeight = function (src, callback, i) {
        if (!src) {
            callback && callback(0);
            return;
        }
        let image = new Image();
        image.src = `${src}?x-oss-process=image/resize,m_lfit,w_100`;
        image.onload = () => {
            const scale = image.height / image.width;
            const proportional = (this.childs.width * scale).toFixed(2);
            callback && callback(Number(proportional));
        };
        image.onerror = () => {
            console.log("加载失败");
            callback && callback(0);
        };
    };

    //渲染数据
    waterfalls.prototype.storageRender = function (list) {
        const loadHtml = [];
        let sotrs = this.getChildsMinHeight();
        let startIndex = 0;
        // console.warn(startIndex)
        for (let i = 0; i < list.length; i++) {
            const index = sotrs[startIndex];
            if (!loadHtml[index]) {
                loadHtml[index] = [];
            }
            list[i]["currentIndex"] = i;
            if (this.renderType === "liquid") {
                loadHtml[index].push(list[i]);
            } else {
                loadHtml[index].push(this.render(list[i]));
            }
            startIndex = startIndex === this.childs.node.length - 1 ? 0 : startIndex + 1;
        }
        this.appendHTml(loadHtml);
    };

    waterfalls.prototype.forAppendHTml = function (loadHtml) {
        for (let i = 0; i < loadHtml.length; i++) {
            this.appendHTml(loadHtml[i].html, loadHtml[i].height);
        }
    };

    //获取瀑布流距离顶部高度
    waterfalls.prototype.getOffsetTop = function () {
        return (this.fallNode.offset() && this.fallNode.offset().top) || 0;
    };

    //获取窗口高度
    waterfalls.prototype.window = function () {
        return $(window).height();
    };

    //获取瀑布流高度
    waterfalls.prototype.getHeight = function () {
        return this.fallNode.height();
    };

    //获取子内容

    waterfalls.prototype.getChilds = function () {
        const node = this.fallNode.children(this.childrenClassName);
        const heights = [];
        for (let i = 0; i < node.length; i++) {
            heights.push({ index: i, height: node.eq(i).height() });
        }
        return {
            node,
            width: node.eq(0).width(),
            heights
        };
    };

    //获取子元素最小的高度
    waterfalls.prototype.getChildsMinHeight = function () {
        const childsHeight = this.childs.heights;
        const copyChildsHeight = JSON.parse(JSON.stringify(childsHeight));
        return copyChildsHeight
            .sort((a, b) => a.height - b.height)
            .map((item) => {
                return item.index;
            });
    };

    //设置高度
    waterfalls.prototype.setHeight = function (index, height) {
        this.childs.heights[index] = this.childs.heights[index] + height + this.addHeight();
    };

    //滚动事件
    waterfalls.prototype.scroll = function (self) {
        //滚动高度
        const scrollTop = $(this).scrollTop();

        if (scrollTop + self.windowH + self.bottomDistance >= self.getHeight() + self.getOffsetTop()) {
            //设置内容高度
            self.childs = self.getChilds();
            //执行回调方法
            self.scrollBottom && self.scrollBottom.call(this);
        }
    };

    waterfalls.prototype.complete = function () {
        moi.bindRedirectHref(this.fallNode.find("a"));
        this.renderComplete && this.renderComplete.call(this, this.fallNode);
    };

    //追加html
    waterfalls.prototype.appendHTml = function (loadHtml) {
        const self = this;
        const childsNode = this.childs.node;
        let completeCount = 0;
        for (let i = 0; i < loadHtml.length; i++) {
            if (loadHtml[i]) {
                //liquid
                if (this.renderType === "liquid") {
                    this.render(loadHtml[i], function (node) {
                        childsNode.eq(i).append(node);
                        self.renderComplete && self.renderComplete.call(this, node);
                    });
                } else {
                    //html追加
                    const node = $(loadHtml[i].join(""));
                    childsNode.eq(i).append(node);
                    self.renderComplete && self.renderComplete.call(this, node);
                }
            }
        }
    };

    //显示加载
    waterfalls.prototype.showLoad = function () {
        this.dom.append(this.loadHtml);
    };

    //隐藏
    waterfalls.prototype.hideLoad = function () {
        this.loadHtml.remove();
    };

    //显示加载完毕
    waterfalls.prototype.showAll = function (text) {
        this.dom.append($(`<div class="mo-waterfall-status-box"><span>${text}</span></div>`));
    };

    //显示加载更多
    waterfalls.prototype.showMore = function (text, href) {
        if (text) {
            this.dom.append($(`<div class="mo-waterfall-status-box"><a href='${href}' class="secondary_btn">${text}</a></div>`));
        }
    };

    window.waterfalls = waterfalls;

    //是否执行滚动加载事件
    window.waterfallsScrollLoad = true;

    window.waterfalls = waterfalls;
})();

(function () {
    // 获取window的引用:
    const $window = $(window);
    // 获取包含data-src属性的img，并以jQuery对象存入数组:
    // const lazyImgs = $('img[data-src]');

    //获取屏幕像素密度
    const ratio = window.devicePixelRatio;
    var lazyImgs = [];

    //加载图片
    function lazyImageShow() {
        // 获取页面滚动的高度:
        const wtop = $window.scrollTop();
        // 判断是否还有未加载的img:
        if (lazyImgs.length > 0) {
            // 获取可视区域高度:
            const wheight = $window.height();
            const delIndexs = [];
            for (let i = 0; i < lazyImgs.length; i++) {
                const $i = lazyImgs[i];
                // 判断是否在可视范围内:
                if ($i.offset().top - wtop < wheight) {
                    // 设置src属性:
                    const imgSrc = $i.attr("data-src");
                    //裁剪方法
                    const cropType = $i.attr("data-crop-type");

                    //裁剪高度
                    const cropHeight = $i.attr("data-crop-height");

                    //裁剪宽带
                    const cropWidth = $i.attr("data-crop-width");

                    //如果有src属性加载src
                    if (imgSrc) {
                        const splitStr = imgSrc.indexOf("?") === -1 ? "?" : "&";
                        const fileExtension = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);

                        //默认宽度裁剪
                        const width = cropWidth || $i.innerWidth();
                        let src = width ? `${imgSrc}${splitStr}x-oss-process=image/resize,m_lfit,w_${Math.floor(width * (ratio > 1 ? ratio : 1.5))}` : imgSrc;

                        //高度裁剪
                        if (cropType === "height") {
                            const height = cropHeight || $i.height();
                            src = height ? `${imgSrc}${splitStr}x-oss-process=image/resize,m_lfit,h_${Math.floor(height * (ratio > 1 ? ratio : 1.5))}` : imgSrc;
                        }

                        //如果图片等于 webp 、svg 、icon、ico 不裁剪
                        if (fileExtension === "webp" || fileExtension === "svg" || fileExtension === "icon" || fileExtension === "ico") {
                            src = imgSrc;
                        }

                        delIndexs.push(i);
                        $i.attr("src", src);
                        $i[0].removeAttribute("data-src");
                        $i[0].onerror = function () {
                            $i.attr("src", "/theme/default/assets/empty.png");
                            console.log("加载图片失败", src);
                        };
                        $i[0].onload = function () {
                            moi.addEvent(
                                "lazyImg",
                                function () {
                                    this.params = src;
                                },
                                true
                            );
                            moi.onEvent("lazyImg");
                        };
                    } else {
                        $i[0].removeAttribute("data-src");
                    }
                }
            }

            for (let i = delIndexs.length - 1; i >= 0; i--) {
                lazyImgs.splice(delIndexs[i], 1);
            }
        }
    }

    // 定义事件函数:
    const onScroll = function () {
        lazyImageShow();
    };

    // 绑定事件:
    $window.scroll(onScroll);

    //懒加载
    window.lazyImageShow = lazyImageShow;

    window.addLazyImages = function (nodes) {
        nodes.each(function () {
            lazyImgs.push($(this));
        });
        onScroll();
    };
})();

$(function () {
    // function getDefaultStyle(obj,attribute){ // 返回最终样式函数，兼容IE和DOM，设置参数：元素对象、样式特性
    //     return obj.currentStyle?obj.currentStyle[attribute]:document.defaultView.getComputedStyle(obj,false)[attribute];
    // }

    //商品列表图片比例
    function imgSize(productBox) {
        // const productBox = $(classID);
        for (let i = 0; i < productBox.length; i++) {
            var self = productBox.eq(i);

            var data_scale = self.data("scale").toString();
            var productWidth = parseInt(self.width());
            if (data_scale === "none" || !productWidth) {
                return false;
            }

            switch (data_scale) {
                case "11":
                    self.css("height", productWidth);
                    break;
                case "34":
                    self.css("height", (productWidth / 3) * 4);
                    break;
                case "23":
                    self.css("height", (productWidth / 2) * 3);
                    break;
                case "12":
                    self.css("height", (productWidth / 1) * 2);
                    break;
                default:
                    break;
            }

            self.attr("data-scale", "none");
        }
    }
    $(window).resize(function () {
        imgSize($(".product_img"));
    });

    $(window).load(function () {
        imgSize($(".product_img"));
    });
    imgSize($(".product_img"));

    window.productSize = imgSize;

    $("img").error(function () {
        $(this).attr("src", "/theme/default/assets/empty.png");
    });
});

//全局搜索方法
window.oemsaasSearch = function (classStr) {
    const themeId = moi.getUrlParam("theme_id");
    const searchValue = $(classStr).val();
    if (!searchValue) {
        return false;
    }
    const URL = location.href.indexOf("/search") > -1 ? location.href : "/search";
    location.href = moi.setUrlParams(URL, { q: searchValue, page: 1, theme_id: themeId });
    return false;
};

/**
 * 补0
 * @param {type} num
 * @param {type} n
 * @returns {String}
 */
function pad(num, n) {
    return num + Array(n > num ? n - ("" + num).length + 1 : 0).join(0);
}
/**
 * number_format
 * @param number 传进来的数,
 * @param decimals 保留的小数位,默认保留两位小数,
 * @param dec_point 小数点符号
 * @param thousands_sep 为整数位每3位间隔符号
 */
function number_format(number, decimals, dec_point, thousands_sep) {
    var gapnum = 3;
    var str = "";

    number = number.toFixed(decimals); //格式化
    is_negative = number < 0 ? true : false;
    if (is_negative) {
        number = Math.abs(number) + "";
    }
    realnum = number.split(".")[0]; //整数位(使用小数点分割整数和小数部分)
    decimal = number.split(".")[1]; //小数位
    realnumarr = realnum.split(""); //将整数位逐位放进数组 ["1", "2", "3", "4", "5", "6"]

    //把整数部分从右往左拼接，每gapnum位添加一个thousands_sep符号
    for (var i = 1; i <= realnumarr.length; i++) {
        str = realnumarr[realnumarr.length - i] + str;
        if (i % gapnum == 0) {
            str = thousands_sep + str; //每隔gapnum位前面加指定符号
        }
    }

    //当遇到 gapnum 的倍数的时候，会出现比如 ",123",这种情况，所以要去掉最前面的 thousands_sep
    str = realnum.length % gapnum == 0 ? str.substr(1) : str;
    //重新拼接实数部分和小数位
    realnum = str + (decimals ? "" + dec_point + "" + (decimal ? pad(decimal, decimals) : pad(0, decimals)) : "");
    if (is_negative) {
        realnum = "-" + realnum;
    }
    return realnum;
}

//浮点数加法运算
function FloatAdd(arg1, arg2) {
    var r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}

//浮点数减法运算
function FloatSub(arg1, arg2, decimal) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //动态控制精度长度
    n = r1 >= r2 ? r1 : r2;
    if (decimal) {
        n = decimal;
    }
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

//浮点数乘法运算
function FloatMul(arg1, arg2, decimal) {
    var m = 0,
        s1 = arg1.toString(),
        s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    } catch (e) {}
    try {
        m += s2.split(".")[1].length;
    } catch (e) {}
    if (decimal) {
        return ((Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m)).toFixed(decimal);
    }
    return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
}

//浮点数除法运算
function FloatDiv(arg1, arg2, decimal) {
    var t1 = 0,
        t2 = 0,
        r1,
        r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    } catch (e) {}
    try {
        t2 = arg2.toString().split(".")[1].length;
    } catch (e) {}
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        if (decimal) {
            return Number((r1 / r2) * pow(10, t2 - t1)).toFixed(decimal);
        }
        return (r1 / r2) * pow(10, t2 - t1);
    }
}
/**
 * 价格格式
 * @param float|string $price
 * @param integer $decimal 默认2位
 * @return float
 */
function price_format(price, decimal = 2) {
    result = price.toFixed(decimal);
    return Number(result);
}
/**
 * 前台使用
 * 基准货币与其他货币之间的转换
 * @param type $price
 * @param type $tobase 是否转为基准货币
 * @return type
 */
function currencyExchange(price, currency, decimal_num) {
    var rate = currency.exchange_rate;
    var decimal = currency.decimal_num; //小数点问题
    if (decimal_num) {
        decimal = decimal_num;
    }
    return price_format(price * rate, decimal);
}

// 货币格式化
function formatMoney(number, currency, exchangeRate) {
    var exchangeRate = exchangeRate || currency.exchange_rate;
    var symbolLeft = currency.symbol_left;
    var symbolRigth = currency.symbol_right;
    var currencyFormat = currency.currency_format;
    var decimalNum = currency.decimal_num;

    var decimalSeparator = ".";
    var thousandsSeparator = "'";
    switch (currencyFormat) {
        case "123,456.78":
            decimalSeparator = ".";
            thousandsSeparator = ",";
            break;
        case "123,456":
            decimalSeparator = "";
            thousandsSeparator = ",";
            decimalNum = 0;
            break;
        case "123.456,78":
            decimalSeparator = ",";
            thousandsSeparator = ".";
            break;
        case "123.456":
            decimalSeparator = "";
            thousandsSeparator = ".";
            decimalNum = 0;
            break;
        case "123'456.65":
        default:
            decimalSeparator = ".";
            thousandsSeparator = "'";
            break;
    }
    number = number || 0;
    number = price_format(number * exchangeRate, decimalNum);

    symbolLeft = symbolLeft != "" ? symbolLeft + " " : "";
    symbolRigth = symbolRigth != "" ? " " + symbolRigth : "";
    return symbolLeft + "" + number_format(number, decimalNum, decimalSeparator, thousandsSeparator) + "" + symbolRigth;
}

function addCartNum() {
    moi.ajax({
        url: "/cart.json",
        type: "GET",
        dataType: "json",
        success: function (res) {
            var num = res.data.item_number;
            const event = new CustomEvent("changeCartNum", {
                detail: {
                    num: num
                }
            });
            document.dispatchEvent(event);
            if (num > 99) {
                num = "99+";
            }
            
            if (res.data.item_number) {
                $("#cart_number").show().html(num);
            }
        }
    });
}
function miniCart() {
    $(".minicart_form").remove();
    $.ajax({
        url: "/cart/mini",
        cache: false,
        dataType: "html",
        success: function (html) {
            $(".minicart_mid").html(html);
        },
        beforeSend: function () {
            $(".minicart_top").after('<div class="minicart_ajaxloading"><svg t="1592222422836" class="order_btn_icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6126"><path d="M512.606821 958.898283c-246.319012 0-446.698738-200.379727-446.698738-446.698738S266.287809 65.479317 512.606821 65.479317c17.73185 0 32.117488 14.385639 32.117488 32.117488s-14.385639 32.117488-32.117488 32.117488c-210.897268 0-382.463762 171.58696-382.463762 382.484228s171.566494 382.463762 382.463762 382.463762 382.484228-171.566494 382.484228-382.463762c0-106.013499-42.384319-204.603935-119.332852-277.558503-12.859889-12.211113-13.403265-32.536021-1.212618-45.416376 12.190647-12.901845 32.536021-13.403265 45.416376-1.212618 89.870844 85.229127 139.365094 200.35926 139.365094 324.187497C959.327048 758.518556 758.925832 958.898283 512.606821 958.898283z" p-id="6127"></path></svg></div>');
        },
        complete: function () {
            $(".minicart_ajaxloading").remove();
        }
    });
}
function createAjaxLoading() {
    $("body").append('<div class="ajaxloading"><svg t="1592222422836" class="order_btn_icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6126"><path d="M512.606821 958.898283c-246.319012 0-446.698738-200.379727-446.698738-446.698738S266.287809 65.479317 512.606821 65.479317c17.73185 0 32.117488 14.385639 32.117488 32.117488s-14.385639 32.117488-32.117488 32.117488c-210.897268 0-382.463762 171.58696-382.463762 382.484228s171.566494 382.463762 382.463762 382.463762 382.484228-171.566494 382.484228-382.463762c0-106.013499-42.384319-204.603935-119.332852-277.558503-12.859889-12.211113-13.403265-32.536021-1.212618-45.416376 12.190647-12.901845 32.536021-13.403265 45.416376-1.212618 89.870844 85.229127 139.365094 200.35926 139.365094 324.187497C959.327048 758.518556 758.925832 958.898283 512.606821 958.898283z" p-id="6127"></path></svg></div>');
}
function removeAjaxLoading() {
    $(".ajaxloading").remove();
}
